package com.ews.onlineexchange.service.startup;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;

import com.ews.onlineexchange.model.AgentService;
import com.ews.onlineexchange.model.Article;
import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ServiceSupplier;

@Singleton
@Startup
@Named
public class ReIndexBean {

	@Inject
	EntityManager entityManager;

	@PostConstruct
	public void init() {
		// reIndexArticle();
	}

	private static final String QUERY_ALL_ARTICLE_EQL = "" + "	SELECT		article	"
			+ "	FROM		Article article	 ";

	public void reIndexArticle() {
		Session session = (Session) entityManager.getDelegate();
		FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_ARTICLE_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

		long count = 0L;
		while (sr.next()) {
			Article article = (Article) sr.get(0);
			ftSession.index(article);
			count++;
			if (count % 1000 == 0)
				ftSession.flushToIndexes();
		}

		ftSession.flushToIndexes();
		sr.close();
	}

	private static final String QUERY_ALL_BULLETIN_EQL = "" + "	SELECT		b	" + "	FROm		Bulletin b	";

	public void reIndexBulletin() {
		Session session = (Session) entityManager.getDelegate();
		FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_BULLETIN_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

		long count = 0L;
		while (sr.next()) {
			Bulletin _b = (Bulletin) sr.get(0);
			ftSession.index(_b);
			count++;
			if (count % 1000 == 0)
				ftSession.flushToIndexes();
		}

		ftSession.flushToIndexes();
		sr.close();
	}

	private static final String QUERY_ALL_SERVICE_SUPPLIER_EQL = "" + "	SELECT		ss	"
			+ "	FROm		ServiceSupplier ss	";

	public void reIndexServiceSupplier() {
		Session session = (Session) entityManager.getDelegate();
		FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_SERVICE_SUPPLIER_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

		long count = 0L;
		while (sr.next()) {
			ServiceSupplier _ss = (ServiceSupplier) sr.get(0);
			ftSession.index(_ss);
			count++;
			if (count % 1000 == 0)
				ftSession.flushToIndexes();
		}

		ftSession.flushToIndexes();
		sr.close();
	}

	private static final String QUERY_ALL_AGENT_SERVICE_EQL = "" + "	SELECT	 	agentService "
			+ "	FROM		AgentService agentService	";

	public void reIndexAgentService() {
		Session session = (Session) entityManager.getDelegate();
		FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_AGENT_SERVICE_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

		long count = 0L;
		while (sr.next()) {
			AgentService _ss = (AgentService) sr.get(0);
			ftSession.index(_ss);
			count++;
			if (count % 1000 == 0)
				ftSession.flushToIndexes();
		}

		ftSession.flushToIndexes();
		sr.close();
	}

	public void reIndexAll() {
		FullTextEntityManager ftem = Search.getFullTextEntityManager(entityManager);
		try {
			ftem.createIndexer().startAndWait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	
}
